// pages/SignIn/SignIn.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qiandaodata:[],
    shifouqiandao:1,
    day1: false,
    day2: false,
    day3: false,
    day4: false,
    day5: false,
    day6: false,
    day7: false
  },

  showExplain:function(){
    wx.showModal({
      title: '活动说明',
      showCancel: false,
      content: '每日签到可随机获得1~9积分\r\n',
      success(res) {
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var this_ = this;
    var data = {
      paraUrl: "/daily-check-in/list/interval?timeInterval=week",
      paraData: ''
    }
    app.getRequest(data, function (res) {
      if (res.data.result == 200) {
        that.setData({
          qiandaodata: res.data.body
        })
        // 判断是否已经签到
        for (var i = 0; i < res.data.body.length;i++){
          // 时间戳转时间
          var dayone_time_cuo = res.data.body[i].createTime
          var chuo_time = new Date(dayone_time_cuo).toDateString();
          // 获取当前时间
          var getDate = new Date().toDateString();
          if (getDate == chuo_time){
            that.setData({
              shifouqiandao: 2
            })
          }
          
        }
        // 获取本周的周一时间戳,从0点开始算
        var now = new Date();
        var nowTime = now.getTime();
        var day = now.getDay();
        var oneDayTime = 24 * 60 * 60 * 1000;
        var MondayTime = nowTime - (day) * oneDayTime;//显示周日
        MondayTime = this_.toDate(MondayTime);
        // for循环判断找出大于等于周一的天数
        for (var i = 0; i < res.data.body.length; i++) {
          var dayone = res.data.body[i].createTime;
          // 后端给的时间转时间戳
          var dayone_time_cuo = dayone;
          // 本周周一转时间戳
          var MondayTime_time_cuo = Date.parse(new Date(MondayTime));
          if (dayone_time_cuo > MondayTime_time_cuo || dayone_time_cuo == MondayTime_time_cuo) {
            // 时间戳转时间
            var chuo_time = new Date(dayone_time_cuo).toDateString();
            //时间转星期
            var week = chuo_time.slice(0, 3)
            switch (week) {
              case 'Mon': this_.setData({ day1: true }); break;
              case 'Tue': this_.setData({ day2: true }); break;
              case 'Wed': this_.setData({ day3: true }); break;
              case 'Thu': this_.setData({ day4: true }); break;
              case 'Fri': this_.setData({ day5: true }); break;
              case 'Sat': this_.setData({ day6: true }); break;
              case 'Sun': this_.setData({ day7: true }); break;
            }
          }
        }
      }
    })
  },
  // 时间戳转时间
  toDate: function (number) {
    var n = number * 1;
    var date = new Date(n);
    var Y = date.getFullYear() + '/';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    return (Y + M + D)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 签到
  qiandao:function(){
    var that = this;
    var data = {
      paraUrl: "daily-check-in/check-in",
      paraData: ''
    }
    app.postRequest(data, function (res) {
      if (res.data.result == 200) {
        wx.showToast({
          title: '签到成功',
          icon: 'success',
          duration: 2000
        })
        that.onShow();
      }
    })
  }
})